package com.project.quiktask;

import static org.junit.Assert.assertEquals;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Vector;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import com.project.quiktask.controller.Controller;
import com.project.quiktask.google.GoogleCal;
import com.project.quiktask.util.Result;

public class GUIInterface {
	JFrame frame;
	JButton buttonSync;
	JButton buttonHelp;
	JTextField inputField;
	JPanel panel;
	JPanel bottomPanel;
	JTable table;
	JLabel label;
	DefaultTableModel model;
	Controller controller;
	public GUIInterface() {
		// TODO Auto-generated constructor stub
		createAndShowGUI();
		controller=new Controller();
	}

	private void createAndShowGUI() {
		frame = new JFrame();
		panel = new JPanel();
		label=new JLabel("Hello");
		buttonSync = new JButton("SYNC");
		buttonSync.addActionListener(new SyncListener());
		buttonHelp = new JButton("HELP");
		buttonHelp.addActionListener(new HelpListener());
		inputField = new JTextField();
		inputField.addActionListener(new InputListener());
		panel.setBackground(Color.LIGHT_GRAY);
		panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
		model=new DefaultTableModel();		
		table = new JTable(model);
		model.addColumn("*");
		model.addColumn("ID");
		model.addColumn("Description");
		model.addColumn("Start");
		model.addColumn("End");
		model.addColumn("Deadline");
		
		TableColumn col = null;
		int[] width = { 5, 5, 90, 30, 30, 40 };
		for (int i = 0; i < 6; ++i) {
			col = table.getColumnModel().getColumn(i);
			col.setPreferredWidth(width[i]);
		}
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		bottomPanel=new JPanel();
		bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.Y_AXIS));
		panel.add(buttonSync);
		panel.add(buttonHelp);
		bottomPanel.add(inputField);
		bottomPanel.add(label);
		frame.getContentPane().add(BorderLayout.NORTH, panel);
		frame.getContentPane().add(BorderLayout.CENTER, scrollPane);
		frame.getContentPane().add(BorderLayout.SOUTH, bottomPanel);
		frame.pack();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(500, 500);
		inputField.requestFocusInWindow();
		frame.setVisible(true);
	}
	
	public void displayResult(Result result){
		for(int i=model.getRowCount()-1;i>=0;i--){
			model.removeRow(i);
		}
		assertEquals(0, model.getRowCount());
		if(result.getTasksToBeDisplayed()!=null){
		Vector<Task> tasksDisplayed=result.getTasksToBeDisplayed(); 
		for(int i=0;i<tasksDisplayed.size();i++){
			model.insertRow(i, tasksDisplayed.get(i).convertTaskData(i+1));
		}
		}
		label.setText(result.getFeedback());
		
	}
	
	class SyncListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent arg0) {
		
			GoogleCal googleService = new GoogleCal();
			try {
				googleService.run();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

	class HelpListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub

		}

	}

	class InputListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			// TODO Auto-generated method stub
			String msg = inputField.getText();
			Result result=controller.processCommand(msg);
			inputField.selectAll();
			inputField.setText("");
			displayResult(result);
		}
	}
	

}
